const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

function showLoading(text){
  wx.showLoading({
    title: text
  });
}

function hideLoading(){
  wx.hideLoading();
}

function showSuccess(text){
  wx.showToast({
    title: text,
    icon: 'success',
  });
}

function showFail(text){
  wx.showToast({
    title: text,
    icon: 'error',
  });
}

function showToast(text){
  wx.showToast({
    title: text,
    icon: 'none'
  });
}

function goBack() {
  wx.navigateBack();
}

function saveAppValue(key, value) {
  wx.setStorageSync(key, value);
}

function getAppValue(key) {
  return wx.getStorageSync(key);
}

function stopRefresh() {
  wx.stopPullDownRefresh();
}

function showCommonModal(title,msg, callback) {
  wx.showModal({
    title: title,
    content: msg,
    showCancel: true,
    success(res) {
      callback(res)
    }
  })
}

module.exports = {
  formatTime,
  showLoading,
  hideLoading,
  showSuccess,
  showFail,
  showToast,
  goBack,
  saveAppValue,
  getAppValue,
  stopRefresh,
  showCommonModal
}
